package common.tool.box.framework.web.service;

import common.tool.box.common.config.CommonToolBoxConfig;
import common.tool.box.common.constant.Constants;
import common.tool.box.common.core.domain.model.LoginUser;
import common.tool.box.common.utils.SecurityUtils;
import common.tool.box.common.utils.StringUtils;
import common.tool.box.common.utils.file.FileUploadUtils;
import common.tool.box.common.utils.file.FileUtils;
import common.tool.box.system.domain.SysUserFile;
import common.tool.box.system.domain.vo.UserFileStorageVo;
import common.tool.box.system.service.impl.SysUserFileServiceImpl;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 登录密码方法
 * 
 * @author ruoyi
 */
@Component
public class SysFileService
{
    @Value(value = "${user.file.normal.maxCount}")
    private int normalMaxCount;

    @Value(value = "${user.file.silver.maxCount}")
    private int silverMaxCount;

    @Value(value = "${user.file.gold.maxCount}")
    private int goldMaxCount;

    @Autowired
    private SysUserFileServiceImpl sysUserFileService;

    public UserFileStorageVo getUserFilesInfo()
    {
        SysUserFile sysUserFile = new SysUserFile();
        LoginUser user = SecurityUtils.getLoginUser();
        sysUserFile.setUserName(user.getUser().getUserName());
        List<SysUserFile> userFiles = sysUserFileService.selectSysUserFileList(sysUserFile);
        UserFileStorageVo userFileStorageVo = new UserFileStorageVo(userFiles);
        userFileStorageVo.generator();
        userFileStorageVo.analyseUserType(normalMaxCount, silverMaxCount, goldMaxCount);
        return userFileStorageVo;
    }

    /**
     * 根据文件路径修改文件名
     */
    @SneakyThrows
    public int renameByFilePath(String filePath, String newFileName) {
        SysUserFile sysUserFile = new SysUserFile();
        sysUserFile.setFilePath(filePath);
        List<SysUserFile> userFiles = sysUserFileService.selectSysUserFileList(sysUserFile);
        if (userFiles.size() == 1) {
            SysUserFile userFile = userFiles.get(0);
            userFile.setOriginalName(newFileName);
            return sysUserFileService.updateSysUserFile(userFile);
        } else {
            throw new Exception(StringUtils.format("文件路径非法，不允许修改。 ", filePath));
        }
    }

    /**
     * 根据文件路径批量删除文件
     */
    @SneakyThrows
    public int deleteByFilePaths(String[] filePaths) {
        // fileName需要去掉开头的/upload/
        List<String> fileIds = new ArrayList<>();
        for (int i = 0; i < filePaths.length; i++) {
            if (StringUtils.isNotEmpty(filePaths[i]) && filePaths[i].startsWith(Constants.RESOURCE_PREFIX))
            {
                filePaths[i] = filePaths[i].substring(Constants.RESOURCE_PREFIX.length());
            }
            // 取出fileName最后第二个.和最后一个.之间的字符串
            String[] fileNameArray = filePaths[i].split("\\.");
            String fileId = fileNameArray[fileNameArray.length - 2];
            fileIds.add(fileId);
            String filePath = FileUploadUtils.getAbsoluteFile(CommonToolBoxConfig.getProfile() ,filePaths[i]).getAbsolutePath();
            FileUtils.deleteFile(filePath);
        }
        return sysUserFileService.deleteSysUserFileByFileIds(fileIds);
    }
}
